package by.vkhalko.asamo.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.TaskDao;
import by.vkhalko.asamo.datamodel.Task;
import by.vkhalko.asamo.services.TaskService;

@Service
public class TaskServiceImpl implements TaskService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(TaskServiceImpl.class);

	@Inject
	private TaskDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of TaskService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Task get(Long id) {
		Task entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Task task) {
		if (task.getId() == null) {
			LOGGER.debug("Save new: {}", task);
			dao.insert(task);
		} else {
			LOGGER.debug("Update: {}", task);
			dao.update(task);
		}
	}

	@Override
	public void delete(Task task) {
		LOGGER.debug("Remove: {}", task);
		dao.delete(task.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all tasks");
		dao.deleteAll();
	}

}
